package com.zrt.pad;

import java.util.List;

import com.zrt.pad.datasource.bean.GridShowItem;
import com.zrt.pad.padpos.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class WslShowActivity extends Activity implements OnClickListener, OnItemClickListener,
  OnItemLongClickListener, OnEditorActionListener{

	
	private List<GridShowItem> datasource;
	private ListView listbody;
	private BaseAdapter bodyListAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wslshowframe);
		findViewById(R.id.btnok).setOnClickListener(this);
		findViewById(R.id.btncancel).setOnClickListener(this);
		
		bodyListAdapter = new BodyListAdapter(this);
		listbody = (ListView) findViewById(R.id.listbody);
		listbody.setAdapter(bodyListAdapter);
	
		WslShowActivity.this.datasource = null;
	}
	
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		// TODO Auto-generated method stub
		if (event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER) {    
            //do something;
			switch (v.getId()) {
					case R.id.txteptcode: {
						
					}
					break;
					
				    default: 
				      break;
			}
            return true;  
           }  
		return false;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	public class BodyListAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		public BodyListAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}
		
		private class ItemHolder {
			public TextView pluid;
			public TextView plucode;
			public TextView pluname;
			public TextView unit;
			public TextView pfcount;
			public TextView pfprice;
			public TextView plutype;
		}
		
		@Override
		public int getCount() {
	        if(WslShowActivity.this.datasource!=null)
	        {
	        	return WslShowActivity.this.datasource.size();
	        }
	        else {
	        	return 0;
			}
			
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ItemHolder itemHolder = null;
			if (convertView != null) {
				itemHolder = (ItemHolder) convertView.getTag();
			} else {
				convertView = getLayoutInflater().inflate(R.layout.gridlist_item, null);
				itemHolder = new ItemHolder();
				itemHolder.pluid = (TextView) convertView.findViewById(R.id.pluid);
				itemHolder.plucode = (TextView) convertView.findViewById(R.id.plucode);
				itemHolder.pluname = (TextView) convertView.findViewById(R.id.pluname);
				itemHolder.unit = (TextView) convertView.findViewById(R.id.unit);
				itemHolder.pfcount = (TextView) convertView.findViewById(R.id.pfcount);
				itemHolder.pfprice = (TextView) convertView.findViewById(R.id.pfprice);
				itemHolder.plutype = (TextView) convertView.findViewById(R.id.plutype);
				
				convertView.setTag(itemHolder);
			}
			
			GridShowItem gridItem = WslShowActivity.this.datasource.get(position);
			itemHolder.pluid.setText(gridItem.pluid);
			itemHolder.plucode.setText(gridItem.plucode);
			itemHolder.pluname.setText(gridItem.pluname);
			itemHolder.unit.setText(gridItem.unit);
			itemHolder.pfcount.setText(gridItem.pfcount.toString());
			itemHolder.pfprice.setText(gridItem.pfprice.toString());
			itemHolder.plutype.setText(gridItem.plutype);
			
			return convertView;
		}		
	}
}
